package cn.com.woxitech.dev.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class FilesUtils {  
	
    private static FilesUtils instance;  
	public String file;

    
    public FilesUtils (String file){
    	this.file = file;
    	
    }  
  
    public static FilesUtils getInstance(String file) {  
    	instance = new FilesUtils(file);  
    	return instance;  
    }
    
    public String readContents(){
    	
    	String result = "";
        File f = new File(this.file);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
            	result = result + tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    	return result;
    }
    
    public String readContents(String filesname){
    	
    	String result = "";
        File f = new File(filesname);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
            	result = result + tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    	return result;
    }

	public void appendContents(String content) {
		// TODO Auto-generated method stub
		
		FileWriter fw = null;
		try {
			File f=new File(this.file);
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println(content);
		pw.flush();
		
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}	public void appendContents(String file, String content) {
		// TODO Auto-generated method stub
		
		FileWriter fw = null;
		try {
			File f=new File(file);
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println(content);
		pw.flush();
		
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void modifyContents(String filesname, String string, String string2) {
		// TODO Auto-generated method stub
		
    	String result = "";
        File f = new File(filesname);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
            	System.out.println(string + tempString + string2);
            	result = result + tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
	}


}  